
public class Test1 {
    private static int num = 0 ;
    public static void main(String[] args) throws InterruptedException {
        Object lock = new Object() ;
       Thread t1 = new Thread(()->{
           synchronized(lock){
               System.out.println(num++);
               try {
                   Thread.sleep(20000);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
       } , "t1") ;
        Thread t2 = new Thread(()->{

            synchronized(lock){
                System.out.println(num++);
                try {
                    Thread.sleep(20000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "t2") ;
        Thread t3 = new Thread(()->{
            synchronized(lock){
                System.out.println(num++);
                try {
                    Thread.sleep(20000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "t3") ;
        Thread t4 = new Thread(()->{
            synchronized(lock){
                System.out.println(num++);
                try {
                    Thread.sleep(20000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "t4") ;

        t1.start();
        t2.start();
        t3.start();

        t4.start();
         t4.join();

        System.out.println("完成");


    }
}
